package com.afei.bottomtabbar.mvvm.view;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.afei.bottomtabbar.R;
import com.afei.bottomtabbar.base.BaseActivity;

import butterknife.OnClick;

/**
 * @Author： linhuaming
 * @ Date ： 2019/01/09 21:56:56
 * @Email ： linhuaming0603@outlook.com
 */
public class HomeActivity extends BaseActivity {

    @Override
    protected int contentViewLayoutId() {
        return R.layout.activity_home;
    }

    private static final int TAB_COUNT = 5;
    private Fragment[] tabs = new Fragment[TAB_COUNT];
    private int lastIndex = -1;

    @OnClick(R.id.tab_home)
    public void clickHomeTab(View view) {
        showFragment(0);
    }

    @OnClick(R.id.tab_registered_appointment)
    public void clickRegisteredAppointmentTag(View view) {
        showFragment(1);
    }

    @OnClick(R.id.tab_center)
    public void clickCenterTab(View view) {
        showFragment(2);
    }

    @OnClick(R.id.tab_wisdom_clinic)
    public void clickWisdomClinicTab(View view) {
        showFragment(3);
    }

    @OnClick(R.id.tab_mine)
    public void clickMineTab(View view) {
        showFragment(4);
    }

    private void showFragment(int index) {
        if (lastIndex == index) {
            return;
        }
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (lastIndex != -1) {
            ft.hide(tabs[lastIndex]);
        }
        if (tabs[index] == null) {
            switch (index) {
                case 0:
                    tabs[index] = new HomeFragment();
                    break;
                case 1:
                    tabs[index] = new RegisteredAppointmentFragment();
                    break;
                case 2:
                    tabs[index] = new CenterFragment();
                    break;
                case 3:
                    tabs[index] = new WisdomClinicFragment();
                    break;
                case 4:
                    tabs[index] = new MineFragment();
                    break;
            }
            ft.add(R.id.fl_home_container, tabs[index]);
        } else {
            ft.show(tabs[index]);
        }

        ft.commitAllowingStateLoss();
        lastIndex = index;
    }

    @Override
    protected void initView() {

    }
}
